<template>
  <div class="ssh-container" ref="terminal" onmouseup="myFunction"></div>
</template>

<script setup lang="ts">

</script>

<script lang="ts">
import { Terminal } from 'xterm'
import { FitAddon } from 'xterm-addon-fit'
import 'xterm/css/xterm.css'
import {ElMessage} from "element-plus";
import { debounce } from 'lodash'

const packResize = (cols, rows) =>
    JSON.stringify({
      type: 'resize',
      cols: cols,
      rows: rows
    })

export default {
  name: 'Terminal',
  props: ["ip"],
  created() {
    this.form.ip = this.$route.query.ip || this.ip
    this.form.port = this.$route.query.port || '22'
    this.form.user = this.$route.query.user || 'root'
    this.form.pwd = this.$route.query.pwd || 'admin@123'
  },
  data() {
    var validate = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('不能为空'));
      } else {
        callback();
      }
    };
    return {
      rules: {
        ip: [
          {validator: validate, trigger: 'blur'}
        ],
        port: [
          {validator: validate, trigger: 'blur'}
        ],
        user: [
          {validator: validate, trigger: 'blur'}
        ],
        pwd: [
          {validator: validate, trigger: 'blur'}
        ],
      },
      activeName: 'first',
      initText: '连接中...\r\n',
      first: true,
      term: null,
      fitAddon: null,
      ws: null,
      socketUrl: 'ws://localhost:8080/ws',
      form: {
        user: '',
        pwd: '',
        ip: '',
        port: '',
      },
      option: {
        lineHeight: 1.0,
        cursorBlink: true,
        cursorStyle: 'block', // 光标样式 'block' | 'underline' | 'bar'
        fontSize: 14,
        fontFamily: "Monaco, Menlo, Consolas, 'Courier New', monospace",
        theme: {
          background: '#181d28'
        },
        cols: 30 // 初始化的时候不要设置fit，设置col为较小值（最小为可展示initText初始文字即可）方便屏幕缩放
      }
    }
  },
  mounted() {
    this.initWs()
  },

  beforeDestroy() {
    this.removeResizeListener()
    this.term && this.term.dispose()
  },
  methods: {
    initWs() {
      this.initTerm()
      this.initSocket()
      this.onTerminalResize()
      this.onTerminalKeyPress()
      this.MouseEvent()
    },
    myFunction(){
      var selection = window.getSelection();
      var str = selection.toString()
      navigator.clipboard.writeText(str)
      if (str.length != 0 ){
        ElMessage({
          type: 'text',
          message: "已复制"+str,
          showClose: true,
        })
      }
    },
    SendMsg (msg) {
      this.isWsOpen() && this.ws.send(JSON.stringify({
        type: 'stdin',
        data: this.utf8_to_b64(msg)
      }))
    },
    MouseEvent(){
      var _this = this;
      document.addEventListener('contextmenu', function (event) {
        event.preventDefault(); // 阻止默认的右键菜单事件
      });
      document.addEventListener('mouseup', function(event) {
        if (event.button === 1 || event.button === 2) { // event.button表示按下的鼠标按钮编号，2表示右键
          navigator.clipboard
              .readText()
              .then((clipText) => _this.SendMsg(clipText)
              );
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.activeName = 'second'
          this.initWs()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClick(tab, event) {
      if (tab.name === 'second') {
        // this.init()
      }
    },
    utf8_to_b64(rawString) {
      return btoa(unescape(encodeURIComponent(rawString)));
    },
    b64_to_utf8(encodeString) {
      return decodeURIComponent(escape(atob(encodeString)));
    },
    bytesHuman(bytes, precision) {
      if (!/^([-+])?|(\.\d+)(\d+(\.\d+)?|(\d+\.)|Infinity)$/.test(bytes)) {
        return '-'
      }
      if (bytes === 0) return '0';
      if (typeof precision === 'undefined') precision = 1;
      const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB', 'BB'];
      const num = Math.floor(Math.log(bytes) / Math.log(1024));
      const value = (bytes / Math.pow(1024, Math.floor(num))).toFixed(precision);
      return `${value} ${units[num]}`
    },
    isWsOpen() {
      return this.ws && this.ws.readyState === 1
    },
    initTerm() {
      this.term = new Terminal(this.option)
      this.fitAddon = new FitAddon()
      this.term.loadAddon(this.fitAddon)
      this.term.open(this.$refs.terminal)
      // this.fitAddon.fit() // 初始化的时候不要使用fit
      setTimeout(() => {
        this.fitAddon.fit()
      }, 500); // 必须延时处理
    },
    onTerminalKeyPress() {
      this.term.onData(data => {
        this.isWsOpen() && this.ws.send(JSON.stringify({
          type: 'stdin',
          data: this.utf8_to_b64(data)
        }))
      })
    },
    resizeRemoteTerminal() {
      const { cols, rows } = this.term
      console.log('列数、行数设置为：', cols, rows)
      // 调整后端终端大小 使后端与前端终端大小一致
      this.isWsOpen() && this.ws.send(packResize(cols, rows))
    },
    onResize: debounce(function () {
      this.fitAddon.fit()
    }, 500),
    onTerminalResize() {
      window.addEventListener('resize', this.onResize)
      this.term.onResize(this.resizeRemoteTerminal)
    },
    removeResizeListener() {
      window.removeEventListener('resize', this.onResize)
    },
    initSocket() {
      this.term.write(this.initText)
      this.ws = new WebSocket(this.socketUrl,['webssh'])
      this.onOpenSocket()
      this.onCloseSocket()
      this.onErrorSocket()
      this.term._initialized = true
      this.onMessageSocket()
    },
    onOpenSocket() {
      this.ws.onopen = () => {
        console.log('websocket 已连接')
        this.ws.send(JSON.stringify({ type: "addr", data: this.utf8_to_b64(this.form.ip + ':' + this.form.port) }));
        // socket.send(JSON.stringify({ type: "term", data: utf8_to_b64("linux") }));
        this.ws.send(JSON.stringify({ type: "login", data: this.utf8_to_b64(this.form.user) }));
        this.ws.send(JSON.stringify({ type: "password", data: this.utf8_to_b64(this.form.pwd) }));
        this.term.reset()
        setTimeout(() => {
          this.resizeRemoteTerminal()
        }, 500)
      }
    },
    onCloseSocket() {
      this.ws.onclose = () => {
        console.log('关闭连接')
        this.term.write("未连接， 刷新后重连...\r\n");
        // setTimeout(() => {
        //   this.initSocket();
        // }, 3000)
      }
    },
    onErrorSocket() {
      this.ws.onerror = () => {
        this.term.write('连接失败，请刷新！\n')
      }
    },
    onMessageSocket() {
      this.ws.onmessage = res => {
        const msg = JSON.parse(res.data)
        const term = this.term
        // console.log("receive: " + data)
        // 第一次连接成功将 initText 清空
        if (this.first) {
          this.first = false
          term.reset()
          term.element && term.focus()
          this.resizeRemoteTerminal()
        }
        term.write(this.b64_to_utf8(msg.data))
      }
    }
  }
}
</script>

<style lang="scss">
body {
  margin: 0;
  padding: 0;
}

.ssh-container {
  overflow: hidden;
  height: 85vh;
  border-radius: 4px;
  background: rgb(24, 29, 40);
  padding: 0 5px;
  color: rgb(255, 255, 255);

  .xterm-scroll-area::-webkit-scrollbar-thumb {
    background-color: #b7c4d1;
    /* 滚动条的背景颜色 */
  }
}
</style>
